//
//  TSBugTapSameViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "TSBugTapSameViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CJUIKitAlertUtil.h>
#import <CQDemoKit/CQTSRipeTableView.h>
#import <CQDemoKit/CQTSButtonFactory.h>

#import "UIViewController+TSBug.h"

@interface TSBugTapSameViewController () <UIGestureRecognizerDelegate> {
    
}

@property (nonatomic, assign, readonly) BOOL fixBug;    /**< 是否修复bug（默认否） */

@property (nonatomic, strong) UITapGestureRecognizer *tapGesture1;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture2;

@end

@implementation TSBugTapSameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(@"同一个view上添加两个相同的手势", nil);
    
    UIButton *bugStateButton = [CQTSButtonFactory bugButtonWithFixBugHandle:^{
        [self fixBug:NO];
    } reproduceBugHandle:^{
        [self fixBug:YES];
    }];
    [self.view addSubview:bugStateButton];
    [bugStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    
    UILabel *knowledgeLabel = [self knowledgeLabel];
    knowledgeLabel.text = @"1、默认一个view里同时注册两个相同的手势，只会响应后添加的那个手势。\n2、但通过shouldRecognizeSimultaneouslyWithGestureRecognizer可达到两个手势都执行";
    [self.view addSubview:knowledgeLabel];
    [knowledgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(bugStateButton.mas_bottom).mas_offset(10);
        //make.height.mas_equalTo(160);
    }];
    
    
    
    UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankViewAction:)];
    tapGesture1.delegate = self;
    [self.view addGestureRecognizer:tapGesture1];
    self.tapGesture1 = tapGesture1;
    
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankViewAction:)];
    tapGesture2.delegate = self;
    [self.view addGestureRecognizer:tapGesture2];
    self.tapGesture2 = tapGesture2;
}


/** 点击空白区域的事件 */
- (void)tapBlankViewAction:(UITapGestureRecognizer *)tap {
    NSString *message = @"响应了手势...";
    CGPoint centerOffset = CGPointZero;
    if (tap == self.tapGesture1) {
        message = @"响应了手势1";
        centerOffset = CGPointMake(0, -40);
    } else if (tap == self.tapGesture2) {
        message = @"响应了手势2";
        centerOffset = CGPointMake(0, 40);
    } else {
        message = @"响应了其他手势";
        centerOffset = CGPointMake(0, 40);
    }
    
    [self __showToast:message centerOffset:centerOffset];
}

#pragma mark - Event
/*
 *  是否要修复bug
 *
 *  @param shouldFixBug 是否要修复bug（YES:修复bug; NO:重现bug）
 */
- (void)fixBug:(BOOL)shouldFixBug {
    _fixBug = shouldFixBug;
}


#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (self.fixBug == NO) {
        return NO; // 不处理此方法的时候，其默认值为NO
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
